package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.MySeachFriendListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseConfirmDialogFragment;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.MySeachFriend;
import com.isunland.managesystem.entity.MySeachFriendOriginal;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.SharedPreferencesUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONException;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/17 18:23
 */
public class AddMyFrendFrament extends BaseListFragment {

  private final int REQUEST_DIALOG = 0;
  private EditText etPersonName;
  private ArrayList<MySeachFriend> mList;
  private TextView tvSearch;
  private MySeachFriend mySeachFriend;
  private MySeachFriendListAdapter mAdapter;

  @Override protected void isPostStart(boolean isPostStart, boolean isShowEmpty) {
    super.isPostStart(false, true);
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.query);
    mList = new ArrayList<>();
    mAdapter = new MySeachFriendListAdapter(getActivity(), mList);
    setListAdapter(mAdapter);
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    View header =
        getLayoutInflater(savedInstanceState).inflate(R.layout.header_search_friend, null, false);
    etPersonName = (EditText) header.findViewById(R.id.et_search_searchPersonHeader);
    tvSearch = (TextView) header.findViewById(R.id.tv_search);
    mListview.addHeaderView(header);
    tvSearch.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        if (!TextUtils.isEmpty(etPersonName.getText().toString())) {
          //按账号搜索人员
          volleyPost();
        } else {
          ToastUtil.show("请输入账号");
        }
      }
    });
  }

  @Override public String getUrl() {
    return ApiConst.URL_MY_FRIEND_SEARCH;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("toAddNumber", etPersonName.getText().toString());
    return params;
  }

  @Override public void resolveResponse(String arg0) {
    MySeachFriendOriginal original = new Gson().fromJson(arg0, MySeachFriendOriginal.class);

    if (original.getResult() == Base.SUCCESS) {
      //获取列表成功
      if (original.getRows() != null) {
        ArrayList<MySeachFriend> mySeachFriend = original.getRows();
        mList.clear();
        mList.addAll(mySeachFriend);
        mAdapter = new MySeachFriendListAdapter(getActivity(), mList);
        setListAdapter(mAdapter);
      } else {
        //用户不存在
        ToastUtil.show(original.getMessage());
        mList.clear();
        mAdapter = new MySeachFriendListAdapter(getActivity(), mList);
        setListAdapter(mAdapter);
      }
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    mySeachFriend = mList.get(position - 2);
    //弹出框,是否添加为好友
    showDialog();
    super.onListItemClick(l, v, position, id);
  }

  private void showDialog() {
    DialogFragment dialog =
        BaseConfirmDialogFragment.newInstance("是否确定添加为好友?", R.string.hintMessage);
    FragmentManager fm = getFragmentManager();
    dialog.setTargetFragment(this, REQUEST_DIALOG);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_DIALOG) {
      //添加为好友
      volleyPostAddFriend();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  private void volleyPostAddFriend() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_EXPERT_ATTENTION);
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("toAddJobno", mySeachFriend.getJobNo());
    params.put("friendOrFollow", "T");//"T"加为好友"F"添加为关注
    params.put("subCode",
        SharedPreferencesUtil.getString(mActivity, SharedPreferencesUtil.KEY_FORUM_SUBCODE,
            ""));//区分农技或农机
    MyUtils.showDialogLoading(getActivity(), "请求添加好友");
    mActivity.volleyPost(url, params.getParams(), new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {

      }

      @Override public void onVolleyResponse(String arg0) throws JSONException {

        MyUtils.dismissDialog();
        try {
          SuccessMessage successsMessage = new Gson().fromJson(arg0, SuccessMessage.class);

          String successType = successsMessage.getResult();

          if (successType.equals("1")) {
            ToastUtil.show(R.string.success_operation);
          } else {
            ToastUtil.show(R.string.failure_operation);
          }
        } catch (Exception e) {
          LogUtil.e("error", e);
          ToastUtil.show(R.string.failure_operation);
        }
      }
    });
  }
}

